// src/stores/counter.js
import { defineStore } from "pinia";

// defineStore() 第一个参数是唯一的 store ID
export const useCounterStore = defineStore("counter", {
  // 状态 (State)
  state: () => ({
    menu: [
      {
        path: "/title/1",
        name: "题目1",
        title: "您是否已阅读并理解公司《员工保密协议》的内容？",
        // 1 单选 2 多选
        radio: "1",
        // type:1情绪问答题 2问答陈述题
        type: "1",
        list: [
          {
            key: 1,
            text: "是，我已阅读并理解",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 2,
            text: "否，我尚未阅读",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 3,
            text: "部分理解，需要进一步说明",
            isBgc: false,
            isCorrect: false,
          },
        ],
        // type:1情绪问答题 2问答陈述题 ，type == 2时候 answerRemark 才有效
        answerRemark: "",
        answer: "",
        isSave: false,
      },
      {
        path: "/title/2",
        name: "题目2",
        title: "在签署合同时，以下哪些条款您认为必须明确约定？",
        // 1 单选 2 多选
        radio: "1",
        // type:1情绪问答题 2问答陈述题
        type: "2",
        list: [
          {
            key: 1,
            text: "双方权利义务",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 2,
            text: "违约责任",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 3,
            text: "争议解决方式（如仲裁或诉讼）",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 4,
            text: "合同生效条件和期限",
            isBgc: false,
            isCorrect: false,
          },
        ],
        // type:1情绪问答题 2问答陈述题 ，type == 2时候 answerRemark 才有效
        answerRemark: "",
        answer: "",
        isSave: false,
      },
      {
        path: "/title/3",
        name: "题目3",
        title:
          "根据《中华人民共和国劳动合同法》，以下哪些属于劳动合同中必须具备的条款？",
        // 1 单选 2 多选
        radio: "1",
        // type:1情绪问答题 2问答陈述题
        type: "2",
        list: [
          {
            key: 1,
            text: "劳动合同期限",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 2,
            text: "工作内容和工作地点",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 3,
            text: "工资待遇和支付方式",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 4,
            text: "试用期约定",
            isBgc: false,
            isCorrect: false,
          },
        ],
        // type:1情绪问答题 2问答陈述题 ，type == 2时候 answerRemark 才有效
        answerRemark: "",
        answer: "",
        isSave: false,
      },
    ],
    question: [],
  }),

  actions: {
    updateTitle(title, form) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.title = title;
        }
        return m;
      });
    },

    updateList(value, form, key) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.list = m.list.map((n) => {
            if (n.key == key) {
              n.text = value;
            }
            return n;
          });
        }
        return m;
      });
    },

    updateCorrect(value, form, key) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.list = m.list.map((n) => {
            if (n.key == key) {
              n.isCorrect = value;
            }
            return n;
          });
        }
        return m;
      });
    },

    updateMenuType(type, form) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.type = type;
        }
        return m;
      });
    },

    updateMenuRadio(radio, form) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.radio = radio;
        }
        return m;
      });
    },

    updateMenu(value, form, logicValue) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.list.map((n) => {
            if (n.key == value.key) {
              n.logic = logicValue;
            }
            return n;
          });
        }
        return m;
      });
    },

    updateMenutitleKey(value, form, titleKeyValue) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.list.map((n) => {
            if (n.key == value.key) {
              n.titleKey = titleKeyValue;
            }
            return n;
          });
        }
        return m;
      });
    },
    updateMenuRemark(value, form, remarkKey) {
      this.menu = this.menu.map((m) => {
        if (form.path == m.path) {
          m.list.map((n) => {
            if (n.key == value.key) {
              n.remark = remarkKey;
            }
            return n;
          });
        }
        return m;
      });
    },
    addMenu() {
      const index = this.menu.length + 1;
      this.menu.push({
        path: `/title/${index}`,
        name: `题目${index}`,
        title: "",
        // 1 单选 2 多选
        radio: "1",
        // type:1情绪问答题 2问答陈述题
        type: "1",
        list: [
          {
            key: 1,
            text: "",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 2,
            text: "",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 3,
            text: "",
            isBgc: false,
            isCorrect: false,
          },
          {
            key: 4,
            text: "",
            isBgc: false,
            isCorrect: false,
          },
        ],
        // type:1情绪问答题 2问答陈述题 ，type == 2时候 answerRemark 才有效
        answerRemark: "",
        answer: "",
        isSave: false,
      });
    },
    saveMenu(path, answer, formtitle) {
      this.menu = this.menu.map((item) => {
        if (item.path === path) {
          return {
            ...item,
            title: formtitle,
            answerRemark: answer,
            isSave: true,
          };
        }
        return item;
      });
      console.log(this.menu, "@@@@@@@@@@@");
    },
    updateQuestion(value) {
      this.question = value;
    },

    updateQuestionList(value) {
      this.questionRemark = value;
    },
  },
  // 启用持久化
  persist: true,
});
